package com.podium.web.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.podium.comment.AjaxResult;
import com.podium.comment.annotate.OperateLog;
import com.podium.comment.annotate.PermAuth;
import com.podium.comment.constant.OperateLogType;
import com.podium.web.system.entity.SysConfig;
import com.podium.web.system.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 邵文泽
 * @description 参数配置
 * @date 2025-05-09 09:24:07
 */
@RestController
@RequestMapping("/system/config")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 分页获取参数配置信息
     *
     * @param sysConfig 查询参数配置信息条件
     */
    @PermAuth("system:config:list")
    @GetMapping("page")
    public AjaxResult<IPage<SysConfig>> getPage(@RequestParam(defaultValue = "1") int pageNum,
                                                   @RequestParam(defaultValue = "10") int pageSize,
                                                   SysConfig sysConfig
                                                     ) {
        Page<SysConfig> page = new Page<>(pageNum, pageSize);
        return new AjaxResult<IPage<SysConfig>>().success(sysConfigService.page(page, sysConfig));
    }

    /**
     * 获取参数配置信息
     *
     * @param sysConfig 查询参数配置信息条件
     */
    @PermAuth("system:config:list")
    @GetMapping("list")
    public AjaxResult<List<SysConfig>> getList(SysConfig sysConfig) {
        return new AjaxResult<List<SysConfig>>().success(sysConfigService.list(sysConfig));
    }

    /**
     * 根据ID获取参数配置信息
     */
    @GetMapping("{id}")
    public AjaxResult<SysConfig> getByInfo(@PathVariable Long id) {
        return new AjaxResult<SysConfig>().success(sysConfigService.getById(id));
    }

    /**
     * 添加参数配置信息
     *
     * @param sysConfig 参数配置信息
     */
    @OperateLog(title = "系统管理-参数配置", businessType = OperateLogType.ADD)
    @PermAuth("system:config:add")
    @PostMapping("/save")
    public AjaxResult<Void> save(@RequestBody SysConfig sysConfig) {
        boolean row = sysConfigService.save(sysConfig);
        return row ? new AjaxResult<Void>().success() : new AjaxResult<Void>().error();
    }

    /**
     * 更新参数配置信息
     *
     * @param sysConfig 参数配置信息
     */
    @OperateLog(title = "系统管理-参数配置", businessType = OperateLogType.EDIT)
    @PermAuth("system:config:edit")
    @PostMapping("/update")
    public AjaxResult<Void> update(@RequestBody SysConfig sysConfig) {
        boolean row = sysConfigService.updateById(sysConfig);
        return row ? new AjaxResult<Void>().success() : new AjaxResult<Void>().error();
    }

    /**
     * 删除参数配置信息
     *
     * @param ids 参数配置信息ID集合
     */
    @OperateLog(title = "系统管理-参数配置", businessType = OperateLogType.DELETE)
    @PermAuth("system:config:delete")
    @GetMapping("/remove/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        if (ids == null || ids.length == 0) {
            return new AjaxResult<Void>().error("没有要删除的数据！");
        }
        for (Long id : ids) {
            sysConfigService.removeById(id);
        }
        return new AjaxResult<Void>().success();
    }

}